﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Util;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.Sys;
using HiStaff.Util.Enum;
using System.Collections;
using HiStaff.Domain.PA;
using HiStaff.iSystem;

namespace HiStaff.iPay
{
    public partial class SignList : FormBase
    {
        public SignList()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            base.AddBasicCommandBar();
            InitControl();
            LoadData();
            return base.FormLoad();
        }
        
        protected override bool SaveButtonClick()
        {
            PA_SIGN_LST objSave = new PA_SIGN_LST();
            ValueControlsToObject(objSave, this);
            if (Service.ExcuteStoreNonQuery("APRU_PA_SIGN_LST", objSave))
            {
                if (string.IsNullOrEmpty(objSave.ERRCODE))
                {
                    MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadData();
                    return true;
                }
                else
                {
                    MessageBox.Show(objSave.ERRMSG, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            return false;
        }

        protected override bool DeleteButtonClick()
        {
            if (grdData.SelectedItems.Count > 0)
            {
                var row = grdData.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    if (Service.ExcuteStoreNonQuery("APRD_PA_SIGN_LST", row.GetRow().DataRow))
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadData();
                        return true;
                    }
                }
            }
            return false;
        }

        protected override bool CancelButtonClick()
        {
            BindDataFromGridToForm(grdData, this);
            return true;
        }

        private void LoadData()
        {
            Data.ALLPASIGN = Service.PayrollClient.GetAllSignList();

            var lstData =  Data.ALLPASIGN.FindAll(obj => obj.PATYPE == ConvertHelper.ToDecimal(cboType.SelectedValue) && 
                                                                obj.SALGROUP == ConvertHelper.ToDecimal(cboSalGroup.SelectedValue));

            grdData.DataSource = lstData;
            grdData.Refresh();
        }

        private void InitControl()
        {
            List<SY_DEFAULT_LIST> lstSource;

            List<SY_DATA_TYPE> lstDataType = ConvertHelper.DataTableToList<SY_DATA_TYPE>((DataTable)cboDataType.DataSource);
            grdData.RootTable.Columns["DATATYPE"].ValueList.PopulateValueList(lstDataType, "ID", "DESCRIPTION");


            lstSource = ConvertHelper.DataTableToList<SY_DEFAULT_LIST>((DataTable)cboGroup.DataSource);
            grdData.RootTable.Columns["PAGROUP"].ValueList.PopulateValueList(lstSource, "ID", "NAME");

            lstSource = ConvertHelper.DataTableToList<SY_DEFAULT_LIST>((DataTable)cboMode.DataSource);
            grdData.RootTable.Columns["PAMODE"].ValueList.PopulateValueList(lstSource, "ID", "NAME");
        }

        private void grdData_SelectionChanged(object sender, EventArgs e)
        {
            BindDataFromGridToForm(grdData, this);
        }

        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void cboSalGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
